<?php
require_once("../connection.php");

$gp_mem = $_POST['gp_mem'];
$gp_name = mysql_real_escape_string($_POST['gp_name']);
$gp_visibility = $_POST['gp_visibility'];
$gp_description = mysql_real_escape_string($_POST['gp_description']);
$user = $_POST['user'];
$user_name = $_POST['user_name'];

date_default_timezone_set('Asia/Calcutta');
$timestamp = time();
//echo $gp_mem.$gp_name.$gp_visibility.$gp_description.$user;

// first we need to check if a similar group exists or not
// selecting all the groups with this user
// leaving it here as it does not seem very necessary

$res = mysql_query("INSERT INTO site_groups (`name`,`fbid`,`description`,`visibility`,`timestamp`) VALUES('$gp_name','$user','$gp_description','$gp_visibility','$timestamp')");
if(!$res) echo mysql_error();
//id of the entry in site_groups table
$id = mysql_insert_id();

$res1 = mysql_query("INSERT INTO site_group_newness (`gpid`,`newness`) VALUES('$id','$timestamp')");
if(!$res1) echo mysql_error();

//disseminating the user array
$arr = explode(";",$gp_mem);
//adding the creator as a member
$res = mysql_query("INSERT INTO site_group_members (`gpid`,`fbid`,`username`,`mod`,`del`) VALUES('$id','$user','$user_name','1','0')");
if(!$res) echo mysql_error();

foreach($arr as $mem)
{
	if($mem != "")
	{
		$temp = explode(':',$mem);
	$mem_id = substr($temp[0],8);
	$mem_name = $temp[1];
	$res = mysql_query("INSERT INTO site_group_members (`gpid`,`fbid`,`username`,`mod`,`del`) VALUES ('$id','$mem_id','$mem_name','0','0')");
	if(!$res) echo mysql_error();	
	}
}

echo 'id_'.$id;

?>